<?php
namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use App\Model\AuthGroup;
use App\Model\AuthGroupAccess;
use App\Model\UcenterMember;
use EasySwoole\Validate\Validate;
use App\Model\Member as Members;

class Member extends AdminBase
{
    /**
     * 用户中心
     * @return UcenterMember
     */
    public function getUcenterMemberModel()
    {
        return new UcenterMember();
    }

    /**
     * 用户member
     * @return Members
     */
    public function getMemberModel()
    {
        return new Members();
    }

    /**
     * 用户权限组
     * @return AuthGroupAccess
     */
    public function getAuthGroupAccessModel()
    {
        return new AuthGroupAccess();
    }

    /**
     * 角色组模型
     * @return AuthGroup
     */
    public function getAuthGroupModel(){
        return new AuthGroup();
    }


    /**
     * 首页
     * @return string|void
     */
    public function index()
    {
        if ($this->isPost()) {

            $where = array();
            /**
             * 开始时间
             */
            $start_date = $this->postParam("start_date");
            /**
             * 结束时间
             */
            $end_date = $this->postParam("end_date");
            if (!empty($start_date) && !empty($end_date)) {
                $where["update_time|BETWEEN"] = [strtotime($start_date), strtotime($end_date)];
            }
            /**
             * 帐号
             */
            $account = $this->postParam("account");
            if (!empty($account)) {
                $uid = $this->getUcenterMemberModel()->getUidByAccount($account);
                $where["uid"] = $uid;
            }
            /**
             * 昵称
             */
            $nickname = $this->postParam("nickname");
            if (!empty($nickname)) {
                $where["nickname|like"] = '%' . $nickname . '%';
            }
            $memberList = $this->getMemberModel()->getList($this->postParams(), $where);
            foreach ($memberList['list'] as $k => $v) {
                $memberList['list'][$k]['account'] = $this->getUcenterMemberModel()->getAccountByUid($v['uid']);
                $memberList['list'][$k]['auth_name'] = empty($this->getAuthGroupAccessModel()->getAuthName($v['uid'])) ? '无' : $this->getAuthGroupAccessModel()->getAuthName($v['uid']);
            }
            return $this->writePageJson('操作成功', $memberList);
        }
        return $this->fetch();
    }


    /**
     * 账号新增
     * @return string|void
     */
    public function add()
    {
        if ($this->isPost()) {
            return $this->writeJson("操作成功", $this->getMemberModel()->addMemberInfoRow($this->postParams()));
        }
        return $this->fetch();
    }

    /**
     * 用户编辑
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if ($this->isPost()) {
            return $this->writeJson('修改成功',$this->getMemberModel()->updateMemberInfoRow($this->postParams()));
        }
        $info = $this->getMemberModel()->infoWithData(["uid" => $this->requestParam('id')]);
        if(empty($info)){
            $this->assign(['data' => []]);
        }else{
            $this->assign(['data' => $info[0]]);
        }
        return $this->fetch();
    }

    /**
     * 删除数据
     * @return bool|null
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function del()
    {
        if ($this->getMemberModel()->delData(["uid" => $this->requestParam("uid")])) {
            return  $this->writeJson('删除成功',$this->getUcenterMemberModel()->delData(["id" => $this->requestParam("uid")]));
        }
    }

    /**
     * 重置opt,google验证
     * @return mixed
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function optgg(){
        return $this->writeJson('修改成功',$this->getMemberModel()->editData($this->getMemberModel()->analysisWhere(["uid"=>$this->postParam("uid")]),["otp_gg"=>'']));
    }

    /**
     * 选择用户角色
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function change_role(){
        if($this->isPost()){
            $this->getAuthGroupAccessModel()->delData(["uid"=>$this->postParam("uid")]);
            return $this->writeJson('操作成功',$this->getAuthGroupAccessModel()->updateOrCreateData($this->postParams()));
        }
        $uid=$this->getParam('uid');
        $groupList=$this->getAuthGroupModel()->getAll([]);
        $this->assign(['groupRoleList'=>$groupList]);
        $groupId=$this->getAuthGroupAccessModel()->getFiledValue(['uid'=>$uid],'group_id');
        $this->assign(['group_id'=>$groupId]);
        $data =$this->getMemberModel()->findOne(['uid'=>$uid]);
        $udata = $this->getUcenterMemberModel()->findOne(['id'=>$uid]);
        $newdata=array_merge($data,$udata);
        $this->assign(['data'=>$newdata]);
        return $this->fetch('change_role');
    }

    public function change_status(){
        return $this->writeJson('操作成功',$this->getUcenterMemberModel()->changeStatus($this->postParams()));
    }

    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }
}